package com.ymatou.iapi.godmch.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getChannelProvidersBean;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymatou.iapi.godmch.service.getChannelProvidersCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.serviceRelation;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.serviceRelationWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取渠道商的所有服务商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getChannelProviders{
	private static getChannelProvidersBean bean;
	private static getChannelProvidersCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取渠道商的所有服务商");
	}

	@Before
	public void caseUp() {
		bean=new getChannelProvidersBean();
		call=new getChannelProvidersCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private  void check_verify() throws JSONException, InterruptedException {
		//验证
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		
		JSONArray resp=call.getProviderList();
		serviceRelationWapper relationWapper=new serviceRelationWapper();
		List<serviceRelation>  relationlst=relationWapper.selectByMchIdServiceType( bean.getMchId(), bean.getServiceType());
		
		Logger.verifyEquals(resp.length(), relationlst.size(), "验证返回的数据与数据库serviceRelation中的一致");
		for (int i = 0; i < relationlst.size(); i++) {
			int providerId=relationlst.get(i).getProviderId();
			
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("providerId"), providerId, "验证第"+(i+1)+"条providerId");
			//查询providerName
			mch_infoWapper mch_infoWapper=new mch_infoWapper();
			List<Integer> mchIds=new ArrayList ();
			mchIds.add(providerId);
			mch_info mchinfo=mch_infoWapper.selectbyMchIds(mchIds).get(0);
			
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("providerName"), mchinfo.getMchTitle(), "验证第"+(i+1)+"条providerName");
			//新增 wuwen 20200205
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("bindTime"), formatter.format(relationlst.get(i).getCreateTime()), "验证第"+(i+1)+"条bindTime");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getChannelProviders_001() {
		Logger.start(true,"正例-获取渠道商的所有服务商-渠道商有1个服务商");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),1);
			
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(1);
			call.setData(bean);
			call.callService();			
			
			 check_verify();
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getChannelProviders_002() {
		Logger.start(true,"反例-获取渠道商的所有服务商-渠道商有2个服务商");
		try {

			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId2=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),1);
			GodmchManageCallService.InitAddServiceRelation(providerId2,mchBean.getMchId(),1);
			
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(1);
			call.setData(bean);
			call.callService();			
			//验证
			 check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getChannelProviders_003() {
		Logger.start(true,"正例-获取渠道商的所有服务商-渠道商有服务商,但不是出口退税服务");
		try {
			createmchBean createmchBean=GodmchManageCallService.InitAddProvider();
			int providerId1=createmchBean.getMchId();
			
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//修改服务商名称
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(providerId1);
			editBean.setMchTitle("测试渠道商的所有服务商_"+TestDataManager.getRandomIntNum(10));
			GodmchManageCallService.editmachCall(editBean);
			
			GodmchManageCallService.InitAddServiceRelation(providerId1,mchBean.getMchId(),2);
			
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(1);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals("{}", call.getResult().toString(), "验证Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getChannelProviders_004() {
		Logger.start(true,"正例-获取渠道商的所有服务商-渠道商没有服务商");
		try {
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			bean.setMchId(mchBean.getMchId());
			bean.setServiceType(1);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals("{}", call.getResult().toString(), "验证Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}